package com.kky.pp.activity.splash

import UserUtil
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.airbnb.lottie.LottieAnimationView
import com.custom.base.config.BaseConfig
import com.custom.base.entity.OkHttpBodyEntity
import com.custom.base.http.OkHttpFromBoy
import com.custom.base.http.OkHttpManager
import com.custom.base.http.SDOkHttpResoutCallBack
import com.custom.base.mvp.MVPBaseActivity
import com.custom.base.util.SDHandlerUtil
import com.custom.base.util.ToastUtil
import com.google.firebase.crashlytics.ktx.crashlytics
import com.google.firebase.ktx.Firebase
import com.gyf.immersionbar.ImmersionBar
import com.kky.pp.BuildConfig
import com.kky.pp.common.Constant
import com.kky.pp.R
import com.kky.pp.databinding.ActivitySplashBinding
import com.kky.pp.entity.IMTokenGetEntity
import com.kky.pp.interceptor.HttpRequestInterceptor
import com.kky.pp.rongyun.RongConfigUtil
import com.kky.pp.util.SystemUtils
import io.rong.imkit.SpName


/**
 * 作者：
 * 时间：
 * 描述：启动页
 */

class SplashActivity : MVPBaseActivity<SplashContract.View, SplashPresenter,ActivitySplashBinding>(ActivitySplashBinding::inflate), SplashContract.View {

    private var retryNum=0
    override fun setFullScreen(): Boolean {
        return true
    }

    override fun bindLayout(): Int {
        return R.layout.activity_splash
    }
    override fun initView() {
        setRongConfig()

        viewBinding.lottieSplashLoginAnim.playAnimation()
        viewBinding.lottieSplashLoginAnim.visibility = View.VISIBLE

        SDHandlerUtil.runOnUiThreadDelayed(Runnable{
            if (!BaseConfig.getInstance.getBoolean(SpName.profileComplete, false) || BaseConfig.getInstance.getString(
                    SpName.token, "") == "") {
                UserUtil.startLogin(mActivity)
            } else {
                if (SystemUtils.isConnected(mActivity)) {
                    viewBinding.linearSplashNetworkErrorContainer.visibility = View.GONE
                    getIMToken()
                } else {
                    viewBinding.lottieSplashLoginAnim.visibility = View.GONE
                    viewBinding.linearSplashNetworkErrorContainer.visibility = View.VISIBLE
                    viewBinding.txtSplashNetworkErrorTryAgain.setOnClickListener {
                        if (SystemUtils.isConnected(mActivity)) {
                            getIMToken()
                        } else {
                            ToastUtil.toast(getString(R.string.ooops_network_error))
                        }


                    }
                }

            }
        }, 2500)

        Firebase.crashlytics.setCrashlyticsCollectionEnabled(!BuildConfig.DEBUG)
    }

    private fun getIMToken() {
        OkHttpManager.instance.requestInterface(object : OkHttpFromBoy {
            override fun addBody(requestBody: OkHttpBodyEntity) {
                requestBody.setPost(Constant.im_token_Url)
            }
        }, object : SDOkHttpResoutCallBack<IMTokenGetEntity>() {
            override fun onSuccess(entity: IMTokenGetEntity) {
                RongConfigUtil.connectIM(entity.data.token)
            }

            override fun onFailure(code: Int, msg: String) {
                if (code == 700) {
                    showToast(mActivity.getString(R.string.account_logout_tip))
                    UserUtil.out(mActivity)
                }else{
                    if (retryNum < 3){
                        getIMToken()
                        retryNum++
                    }
                }
            }
        })
        HttpRequestInterceptor.addInterceptor()
    }
    fun setRongConfig() {
        RongConfigUtil.updatePortrait()
        RongConfigUtil.configurationRong()
    }

    override fun onResume() {
        super.onResume()
        ImmersionBar.with(this).statusBarDarkFont(false).init()
    }

    override fun onDestroy() {
        super.onDestroy()
        viewBinding.lottieSplashLoginAnim.cancelAnimation()
    }


    override val lottieSplashLoginAnims: LottieAnimationView
        get() = viewBinding.lottieSplashLoginAnim
    override val linearSplashNetworkErrorContainers: LinearLayout
        get() = viewBinding.linearSplashNetworkErrorContainer
    override val txtSplashNetworkErrorTryAgains: TextView
        get() = viewBinding.txtSplashNetworkErrorTryAgain
}